package com.woniuxy.tfss.web;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.woniuxy.tfss.AlipayConfig;
import com.woniuxy.tfss.entity.*;
import com.woniuxy.tfss.service.*;
import com.woniuxy.tfss.utils.MyUtils;
import com.woniuxy.tfss.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Controller
@CrossOrigin
public class TestController {
    @Autowired(required = false)
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private RiderService riderService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private DiscountService discountService;

    @Autowired
    private UARecordService userAccountRecordService;


    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private AddressService addressService;



    @PostMapping("recharge/{userId}/{money}")
    @ResponseBody
    public ResponseEntity<?> recharge(@PathVariable Integer userId,@PathVariable Integer money){
        UserAccountRecord ua=new UserAccountRecord();
        ua.setOrderNo(MyUtils.getRechargeNo());
        ua.setMoney(new BigDecimal(money));
        ua.setTime(MyUtils.getNow());
        ua.setUserId(userId);
        ua.setType("1");
        ua.setStatus(UserAccountRecord.PAYING);
        userAccountRecordService.save(ua);
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = ua.getOrderNo();
        //付款金额，必填
        String total_amount = ua.getMoney().toString();
        //订单名称，必填
        String subject = "天府闪送余额充值";
        //商品描述，可空
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = ""; //请求
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            ua.setStatus(UserAccountRecord.PAYFAIL);
            userAccountRecordService.updateById(ua);
            return ResponseEntity.PAYFAILED;
        }
        return new ResponseEntity<>(806, "OK", result);
    }

    @GetMapping("address")
    public String getAddressById(){
        return "address";
    }
    @GetMapping("address/{userId}")
    @ResponseBody
    public ResponseEntity<List<Address>> getAddressByUserId(@PathVariable Integer userId){
        return new ResponseEntity<>(addressService.getAdressByUserId(userId));
    }
}
